package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SpXiangmu;
import com.ruoyi.system.mapper.SpSaichengMapper;
import com.ruoyi.system.domain.SpSaicheng;
import com.ruoyi.system.service.ISpSaichengService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 赛程Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-10
 */
@Service
public class SpSaichengServiceImpl implements ISpSaichengService 
{
    @Autowired
    private SpSaichengMapper spSaichengMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询赛程
     * 
     * @param scId 赛程主键
     * @return 赛程
     */
    @Override
    public SpSaicheng selectSpSaichengByScId(Long scId)
    {
        return spSaichengMapper.selectSpSaichengByScId(scId);
    }

    /**
     * 查询赛程列表
     * 
     * @param spSaicheng 赛程
     * @return 赛程
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpSaicheng> selectSpSaichengList(SpSaicheng spSaicheng)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spSaicheng.setDatascope(datascope);
        if(datascope==4){
            spSaicheng.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spSaicheng.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spSaicheng.setCheckHuiyuanId(0l);
            }

        }
        return spSaichengMapper.selectSpSaichengList(spSaicheng);
    }

    @Override
    public List<SpSaicheng> selectSpSaichengListWeb(SpSaicheng spSaicheng)
    {
        return spSaichengMapper.selectSpSaichengList(spSaicheng);
    }

    /**
     * 新增赛程
     * 
     * @param spSaicheng 赛程
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSpSaicheng(SpSaicheng spSaicheng)
    {
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        spSaicheng.setDeptId(user.getDeptId());
        System.out.println(spSaicheng.getGcDetail());
        spSaicheng.setCreateTime(DateUtils.getNowDate());
        int rows = spSaichengMapper.insertSpSaicheng(spSaicheng);
        insertSpXiangmu(spSaicheng);
        return rows;
    }

    /**
     * 修改赛程
     * 
     * @param spSaicheng 赛程
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSpSaicheng(SpSaicheng spSaicheng)
    {
        System.out.println(spSaicheng.getGcDetail());
        spSaichengMapper.deleteSpXiangmuByScId(spSaicheng.getScId());
        insertSpXiangmu(spSaicheng);
        return spSaichengMapper.updateSpSaicheng(spSaicheng);
    }

    /**
     * 批量删除赛程
     * 
     * @param scIds 需要删除的赛程主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpSaichengByScIds(Long[] scIds)
    {
        spSaichengMapper.deleteSpXiangmuByScIds(scIds);
        return spSaichengMapper.deleteSpSaichengByScIds(scIds);
    }

    /**
     * 删除赛程信息
     * 
     * @param scId 赛程主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpSaichengByScId(Long scId)
    {
        spSaichengMapper.deleteSpXiangmuByScId(scId);
        return spSaichengMapper.deleteSpSaichengByScId(scId);
    }

    /**
     * 新增项目信息
     * 
     * @param spSaicheng 赛程对象
     */
    public void insertSpXiangmu(SpSaicheng spSaicheng)
    {
        List<SpXiangmu> spXiangmuList = spSaicheng.getSpXiangmuList();
        Long scId = spSaicheng.getScId();
        if (StringUtils.isNotNull(spXiangmuList))
        {
            List<SpXiangmu> list = new ArrayList<SpXiangmu>();
            for (SpXiangmu spXiangmu : spXiangmuList)
            {
                spXiangmu.setScId(scId);
                list.add(spXiangmu);
            }
            if (list.size() > 0)
            {
                spSaichengMapper.batchSpXiangmu(list);
            }
        }
    }
}
